/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.dal.hibernate.mysql;

import ezcms.exceptions.DataLayerException;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class HibernateUtil {

    public static void updateObject(Object obj, String errorMessage) throws DataLayerException {
        Session session = HibernateFactory.openSession();
        try {
            Transaction tx = session.beginTransaction();
            session.update(obj);
            tx.commit();
        } catch (Exception ex) {
            throw new DataLayerException(errorMessage, ex);
        } finally {
            HibernateFactory.close(session);
        }
    }

    public static void saveObject(Object obj, String errorMessage) throws DataLayerException {
        Session session = HibernateFactory.openSession();
        try {
            Transaction tx = session.beginTransaction();
            session.saveOrUpdate(obj);
            tx.commit();
        } catch (Exception ex) {
            throw new DataLayerException(errorMessage, ex);
        } finally {
            HibernateFactory.close(session);
        }
    }

    public static void saveObject(Object obj) throws DataLayerException {
        if (obj == null) {
            throw new DataLayerException("Cannot save null object");
        } else {
            saveObject(obj, "Cannot save given object with type = " + obj.getClass().getName());
        }
    }

    public static void deleteObject(Object obj, String errorMessage) throws DataLayerException {
        Session session = HibernateFactory.openSession();
        try {
            Transaction tx = session.beginTransaction();
            session.delete(obj);
            tx.commit();
        } catch (Exception ex) {
            throw new DataLayerException(errorMessage, ex);
        } finally {
            HibernateFactory.close(session);
        }
    }

    public static void deleteObject(Object obj) throws DataLayerException {
        if (obj == null) {
            throw new DataLayerException("Cannot delete null object");
        } else {
            deleteObject(obj, "Cannot delete given object with type = " + obj.getClass().getName());
        }
    }

    public static Object getObjectById(Class type, Serializable id, String errorMessage) throws DataLayerException {
        Session session = HibernateFactory.openSession();
        try {
            return session.get(type, id);
        } catch (Exception ex) {
            throw new DataLayerException(errorMessage, ex);
        } finally {
            HibernateFactory.close(session);
        }
    }

    public static Object getObjectByConditions(Class type, Criterion conditions, String errorMessage)
            throws DataLayerException {
        Session session = HibernateFactory.openSession();
        try {
            Criteria query = session.createCriteria(type);
            if (conditions != null) {
                query.add(conditions);
            }
            return query.uniqueResult();
        } catch (Exception ex) {
            throw new DataLayerException(errorMessage, ex);
        } finally {
            HibernateFactory.close(session);
        }
    }

    public static List getListByConditions(Class type, Criterion conditions, String errorMessage)
            throws DataLayerException {
        Session session = HibernateFactory.openSession();
        try {
            Criteria query = session.createCriteria(type);
            if (conditions != null) {
                query.add(conditions);
            }
            return query.list();
        } catch (Exception ex) {
            throw new DataLayerException(errorMessage, ex);
        } finally {
            HibernateFactory.close(session);
        }
    }
}
